
//Logout
function logout(url){
	parent.location.href = url;
}
//JS跳转
function redirectURL(urlTo){
	window.location.href = urlTo;
}

//后台确认弹出窗
function toConfirm(tips,todoUrl,APP_PATH) {
	jConfirm(tips, 'Confirmation Dialog', function(r) {
		if(r == true){
			$.get(todoUrl,function(result){
				if(result.status == 1){	//成功流程
					jAlert(result.msg);
			    }else if(result.status == 2){	//跳转流程
			    	if(result.msg == null){	//无需页面提示信息
			    		window.location.href = result.data; //直接跳转
			    	}else{
			    		window.location.href = APP_PATH+"/redirect/?msg="+result.msg+"&url="+result.data; //直接跳转
			    	}
			    }else if(result.status == 3){	//JS回调函数流程
			    	eval(result.msg);
			    }else if(result.status == 4){	//刷新当前页
			    	window.location.reload();
			    }else{	//失败流程
			    	jAlert('<strong style="color:red;">'+result.msg+'</strong>');
			    }
				
			},"JSON");
		}
	});
}

//点击ENTER事件，FORM提交JS方法触发
$(function(){
	document.onkeydown = function(e){ 
	    var ev = document.all ? window.event : e;
	    if(ev.keyCode==13) {
	    	if($("#submit_btn")[0] && !$("#popup_container")[0]){	//当错误提示不存在 -> 进行提交
	    		$("#submit_btn").click();
	    	}
	    }
	}
});

/**
 * FORM提交JS截取封装方法
 * @author 王琨
 */
var lock = false;	//锁默认关闭
function doSubmitForm(obj,APP_PATH){
	
	var btn = $(obj);
	var form = btn.closest("form");
	
	var url_addr = form.attr("action");
	var params = form.serialize();
	//加锁判断
	if(lock == true){
		return;
	}
	$.ajax({ url:url_addr, data:params,type:"POST", dataType:"json",beforeSend:function(){
		lock = true;	//加锁
		btn.addClass('submit_loading');
	},success:function(result){
		//解锁---开始------
		lock = false;
		btn.removeClass('submit_loading');
		//解锁---结束------
		if(result.status == 1){	//成功流程
			jAlert(result.msg);
	    }else if(result.status == 2){	//跳转流程
	    	if(result.msg == null){	//无需页面提示信息
	    		window.location.href = result.data; //直接跳转
	    	}else{
	    		window.location.href = APP_PATH+"/redirect/?msg="+result.msg+"&url="+result.data; //直接跳转
	    	}
	    }else if(result.status == 3){	//JS回调函数流程
	    	eval(result.msg);
	    }else if(result.status == 4){	//刷新当前页
	    	window.location.reload();
	    }else{	//失败流程
	    	jAlert('<strong style="color:red;">'+result.msg+'</strong>');
	    }
	}});
	
}

/**
 * uploadify封装函数
 * @param id	要渲染的upload组建
 * @param fileExts	后缀限制，不限制，则赋''，反之可按照如下：'fileTypeExts' : '*.gif; *.jpg; *.png',
 */
function uploadify4j(id, fileExts,timestamp,token) {
	$(id).uploadify({
		'width'    : 64,
		'height'   : 24,
		'buttonText'  :	'浏览上传',
		'fileTypeExts' : fileExts,
		'swf'      : '../../../uploadify.swf',
		'uploader' : '../../../uploadify.php',
		formData: {
            'fileTypeExts' : fileExts,
            'timestamp': timestamp,
            'token': token
        },
        'onInit': function () {
            $(".uploadify-queue").hide();
		},
        'onFallback' : function() {
            alert('您未安装FLASH控件，无法上传组建！请安装FLASH控件后再试。');
        },
        'onUploadProgress' : function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
        	pos = Math.round((bytesUploaded/bytesTotal)*100);
        	var str = "&nbsp;&nbsp;<strong style='color:green;'>上传进度：" + pos + "%</strong>";
        	if(pos >= 100){
        		var str = "&nbsp;&nbsp;<strong style='color:green;'>上传赋值中，请等待...</strong>";
        	}
        	$(id+"_progress").html(str);
        },
        'onUploadSuccess' : function(file, data, response) {
        	var result = $.parseJSON(data);
        	if (result.status == 0) {
                alert(result.msg);
                return;
            }
        	$(id+"_progress").html("&nbsp;&nbsp;<strong style='color:green;'>上传完成</strong>");
        	//以下则为成功上传流程
        	$(id+"_text").val(result.msg);
        	
        }
	});
}
/**
* 级联图片上传封装函数
* @param id	要渲染的upload组建
* @param fileExts	后缀限制，不限制，则赋''，反之可按照如下：'fileTypeExts' : '*.gif; *.jpg; *.png',
*/
function uploadPicRelated(id,id_related, fileExts,url4remote,timestamp,token) {
	$(id).uploadify({
		'width'    : 64,
		'height'   : 24,
		'buttonText'  :	'浏览上传',
		'fileTypeExts' : fileExts,
		'swf'      : '../../../uploadify.swf',
		'uploader' : '../../../uploadify.php',
		formData: {
           'fileTypeExts' : fileExts,
           'timestamp': timestamp,
           'token': token
       },
       'onInit': function () {
           $(".uploadify-queue").hide();
		},
       'onFallback' : function() {
           alert('您未安装FLASH控件，无法上传组建！请安装FLASH控件后再试。');
       },
       'onUploadProgress' : function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
       	pos = Math.round((bytesUploaded/bytesTotal)*100);
       	var str = "&nbsp;&nbsp;<strong style='color:green;'>上传进度：" + pos + "%</strong>";
       	if(pos >= 100){
       		var str = "&nbsp;&nbsp;<strong style='color:green;'>上传赋值中，请等待...</strong>";
       	}
       	$(id+"_progress").html(str);
       },
       'onUploadSuccess' : function(file, data, response) {
	       	var result = $.parseJSON(data);
	       	if (result.status == 0) {
	               alert(result.msg);
	               return;
	        }
	       	$(id+"_progress").html("&nbsp;&nbsp;<strong style='color:green;'>上传完成</strong>");
	       	//以下则为成功上传流程
	       	$(id+"_text").val(result.msg);
	       	//进行ajax同步请求进行复制。
	       	$.ajax({
	       	   type: "POST",
	       	   async:false,
	       	   url: url4remote,
	       	   data: "pic_addr="+result.msg,
	       	   success: function(msg){
	       		   var result2 = $.parseJSON(msg);
		       		if (result2.status == 1) {
		       			$(id_related).val(result2.msg);
		       		}
	       	   }
	       	});
	       	
       }
	});
}


//+添加更多文件 [后台模块]
function addExtraFile(tag,fileType,timestamp,token){
	var $counter = $("#"+tag+"_file_num");
	var file_index = parseInt($counter.html())+1;
	var ext = "";
	if(fileType == "IMG"){
		ext = "*.gif;*.jpg;*.jpeg;*.png";
	}
	var str = '<div id="'+tag+file_index+'_item">'+
					'<input type="text" id="'+tag+file_index+'_text" name="'+tag+'_text[]" size="80" class="text" style="width:220px" readonly/>'+
					'&nbsp;<input id="'+tag+file_index+'" name="extra_pic" type="file" /><span id="'+tag+file_index+'_progress"></span>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:removeExtraFileDiv(\''+tag+file_index+'_item\');">删除</a>'+
					'<script type="text/javascript">'+
						'$(function() {'+
							'uploadify4j("#'+tag+file_index+'","'+ext+'","'+timestamp+'","'+token+'");'+
						'});'+
					'</script><br/>'+
				'</div>';
	$("#"+tag+"_load_div").append(str);
	$counter.html(parseInt($counter.html())+1);
}

//删除展示图片 [图片后台模块]
function removeExtraFileDiv(divId){
	$("#"+divId).remove();
}
//自定义模型【字段类型】下拉框触发事件
$(function(){
	$("[name='form_type']").change(function(){
		var val = $(this).val();
		//.........文本框长度显示
		if(val == "数字" || val == "编辑器" || val == "多图片上传" || val == "多文件上传"){
			$("#length").hide();
		}else{
			$("#length").show();
		}
		//.........文本框长度设置
		if(val == "选项" || val == "多行文本"){
			$("[name='varchar_len']").val(250);
		}else if(val == "文件上传" || val == "图片上传"){
			$("[name='varchar_len']").val(80);
		}else{
			$("[name='varchar_len']").val(50);
		}
		//.........选项列表处理事件
		if(val == "选项"){
			$("#select_val").show();
		}else{
			$("#select_val").hide();
		}
		
	});
});